package com.order.hit.crossover.platform.common.dao;

import com.order.hit.crossover.platform.common.bean.order.Fightorder;
import com.order.hit.crossover.platform.common.bean.Good;
import com.order.hit.crossover.platform.common.bean.Store;
import com.order.hit.crossover.platform.common.bean.order.FightOrderState;
import com.order.hit.crossover.platform.common.utils.TimeChange;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lei
 * @version 0.1
 * @date 2020/9/8
 */

@Getter
@Setter
@ToString
public class FightOrderDao {

    private Long id;

    // store
    private String storeId;
    private String storeName;
    private String storeLocation;

    //拼单发起用户
    private int userId;

    private String userName;

    // 拼单状态
    private String fightOrderState;

    // 拼单的参与人数 ， 要 > 1
    private int userNum;

    // 拼单的现阶段人数，默认为1，即只有发起者
    private  int nowNum;

    // 参与者用户的id，构成的字符串
    private String participaters;

    // good
    private String goodName;
    private int goodNumber;
    private String goodType;

    // 拼单价格,表示为每个人的价格
    private double price;


    private String start_time;
    private String end_time;

    public static FightOrderDao getDao(Fightorder fightorder){
        FightOrderDao fightOrderDao = new FightOrderDao();
        fightOrderDao.setId(fightorder.getId());

        fightOrderDao.setStoreId(fightorder.getStore().getStoreId());
        fightOrderDao.setStoreName(fightorder.getStore().getStoreName());
        fightOrderDao.setStoreLocation(fightorder.getStore().getLocation());

        fightOrderDao.setUserId(fightorder.getUserId());
        fightOrderDao.setUserName(fightorder.getUserName());
        fightOrderDao.setFightOrderState(fightorder.getFightOrderState().toString());
        fightOrderDao.setUserNum(fightorder.getUserNum());
        fightOrderDao.setNowNum(fightorder.getNowNum());
        StringBuilder stringBuilder = new StringBuilder();
        for(int i = 0; i<fightorder.getParticipaters().size();i++){
            if (i != fightorder.getParticipaters().size()-1){
                stringBuilder.append(fightorder.getParticipaters().get(i));
                stringBuilder.append(".");
            } else{
                stringBuilder.append(fightorder.getParticipaters().get(i));
            }
        }
        stringBuilder.deleteCharAt(stringBuilder.lastIndexOf("."));
        fightOrderDao.setParticipaters(stringBuilder.toString());
        fightOrderDao.setGoodName(fightorder.getGood().getGoodName());
        fightOrderDao.setGoodNumber(fightorder.getGoodNumber());
        fightOrderDao.setGoodType(fightorder.getGood().getGoodType());

        fightOrderDao.setPrice(fightorder.getPrice());

        fightOrderDao.setStart_time(TimeChange.getDateString(fightorder.getStart_time()));
        fightOrderDao.setEnd_time(TimeChange.getDateString(fightorder.getEnd_time()));
        return fightOrderDao;
    }

    public static Fightorder getOrder(FightOrderDao fightOrderDao){
        Fightorder fightorder = new Fightorder();
        fightorder.setId(fightOrderDao.getId());
        Store store = new Store();
        store.setStoreId(fightOrderDao.getStoreId());
        store.setLocation(fightOrderDao.getStoreLocation());
        store.setStoreName(fightOrderDao.getStoreName());
        store.setPassword(null);
        fightorder.setStore(store);
        fightorder.setUserId(fightOrderDao.getUserId());
        fightorder.setUserName(fightOrderDao.getUserName());

        fightorder.setFightOrderState(FightOrderState.valueOf(fightOrderDao.getFightOrderState()));
        fightorder.setUserNum(fightOrderDao.getUserNum());
        fightorder.setNowNum(fightOrderDao.getNowNum());

        List<Integer> list = new ArrayList<>();
        String[] pa = fightOrderDao.getParticipaters().split("\\.");
        for (String string: pa){
            list.add(Integer.parseInt(string));
        }
        fightorder.setParticipaters(list);
        Good good = new Good();
        good.setGoodName(fightOrderDao.getGoodName());
        good.setGoodType(fightOrderDao.getGoodType());
        fightorder.setGood(good);

        fightorder.setPrice(fightOrderDao.getPrice());
        fightorder.setGoodNumber(fightOrderDao.getGoodNumber());
        fightorder.setStart_time(TimeChange.getDate(fightOrderDao.getStart_time()));
        fightorder.setEnd_time(TimeChange.getDate(fightOrderDao.getEnd_time()));
        return fightorder;
    }
}
